package cn.bluemobi.platform.service.impl;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.bluemobi.common.core.dto.MapDto;
import cn.bluemobi.common.core.dto.Page;
import cn.bluemobi.common.core.dto.PageCondition;
import cn.bluemobi.platform.core.mybatis.PageUtils;
import cn.bluemobi.platform.core.utils.PlatformConstants;
import cn.bluemobi.platform.entity.system.User;
import cn.bluemobi.platform.entity.tools.Brochure;
import cn.bluemobi.platform.mapper.BrochureMapper;
import cn.bluemobi.platform.service.BrochureService;

/**
 * 
 * @author lvxh
 * 
 */
@Service
@Transactional
public class BrochureServiceImpl implements BrochureService {

    @Autowired
    private BrochureMapper brochureMapper;

    @Override
    public Page<Brochure> findBrochures(PageCondition cond) {
        Page<Brochure> page = PageUtils.startPage(cond);
        page.setList(brochureMapper.findBrochures(cond.getMap()));
        return page;
    }

    @Override
    public MapDto saveOrUpdate(Brochure Brochure, User user) {
        MapDto dto = new MapDto();
        if (StringUtils.isEmpty(Brochure.getBrochureId())) {
            // 新增单页
            brochureMapper.insertBrochure(Brochure);
        } else {
            // 编辑单页
            brochureMapper.updateBrochure(Brochure);
        }
        // userAgreement
        // aboutUs
        // integralRule
        // userHelpCenter
        // employeeHelpCenter
        if (StringUtils.equals("userAgreement", Brochure.getType())) {
            htmlFile("康祺健身隐私策略", Brochure.getContent(), PlatformConstants.IMG_DISK_PATH + "/template/template.html",
                    PlatformConstants.IMG_DISK_PATH + "/template/" + Brochure.getType() + ".html");
        }
        return dto;
    }

    @Override
    public Brochure findBrochureDetail(String brochureId) {
        return brochureMapper.findBrochureDetail(brochureId);
    }

    @Override
    public MapDto deleteBrochure(String brochureId, User user) {
        MapDto dto = new MapDto();
        brochureMapper.deleteBrochure(brochureId);
        return dto;
    }

    public static boolean htmlFile(String title, String content, String filePath, String htmlFile) {
        String str = "";
        long beginDate = (new Date()).getTime();
        try {
            String tempStr = "";
            FileInputStream is = new FileInputStream(filePath);// 读取模块文件
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            while ((tempStr = br.readLine()) != null)
                str = str + tempStr;
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        try {

            str = str.replaceAll("###title###", title);
            str = str.replaceAll("###content###", content);

            File f = new File(htmlFile);
            BufferedWriter o = new BufferedWriter(new FileWriter(f));
            o.write(str);
            o.close();
            System.out.println("共用时：" + ((new Date()).getTime() - beginDate) + "ms");
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
